// pages/address/address.js
let utils = require("../../utils/utils.js");
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		msgList: [],
		cardList: [],
		monthEndTime: '',
		index: 0,
		buyMonthCardUrl: '/myMonthCard/buyMonthCard',
		monthCardListUrl: '/myMonthCard/monthCardList',
		buyStatus: false,
		noBuyStatus: false,
		requestOne: false,
		cardlastImg:'/card.png',
		cardBglastImg:'/walletbg.png',
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getListData();
		this.img();
	},
	img(){
		let cardImg=app.globalData.baseimg+this.data.cardlastImg;
		let cardBgImg=app.globalData.baseimg+this.data.cardBglastImg;
		this.setData({
			cardImg,
			cardBgImg,
		});
	},
	getListData: function() {
		let that = this;
		let indexData = wx.getStorageSync('indexData');
		let userData = indexData.user;
		let _data = {
			url: that.data.monthCardListUrl,
			nonelist: true,
			methods: 'post',
			data: {
				'userId': userData.userId,
				'type': userData.type,
				'deviceId': userData.deviceCode
			}
		};
		utils.request.call(_data, (res) => {
			console.log(res);
			if(res.data.status==200){
				wx.setStorageSync('monthCardInfo', res.data.data);
				let cardImg = [];
				cardImg.push(res.data.data);
				that.data.cardList = cardImg;
				that.data.msgList = res.data.data;
				that.data.cardList.map(function(val, key) {
					if (!val.discountAmount) {
						val.discountAmount = '0.00';
					};
					if (val.isJudge) {
						that.data.noBuyStatus = true;
						that.data.buyStatus = that.data.cardList[0].isJudge;
						if (that.data.buyStatus) {
							that.data.monthEndTime = that.data.cardList[0].monthEndTime;
						};
					};
				});
				console.log(typeof that.data.msgList.isNotLimited);
				that.data.msgList.isNotLimited = Number(that.data.msgList.isNotLimited);
				this.setData({
					cardList: that.data.cardList,
					msgList: that.data.msgList,
					noBuyStatus: that.data.noBuyStatus,
					buyStatus: that.data.buyStatus
				});
			}else{
				let monthCardInfo=res.data.msg;
				wx.setStorageSync('monthCardInfo',monthCardInfo);
				wx.reLaunch({
					url:'../mine/mine'
				});
			};
			
		});
	},
	cardlist(){
		wx.navigateTo({
			url:'../cardRecords/cardRecords'
		});
	},
	buycard: function() {
		let that = this;
		if (that.data.noBuyStatus) {
			utils.tips('您已购买过月卡，不能购买其他月卡了！')
			return false;
		};
		that.data.requestOne = false;
		that.common_request_post(function(res) {
			let parameter = res.data.data;
			that.onBridgeReadyArgments(parameter);
		});
	},
	common_request_post: function(success) {
		let that = this;
		let cardId = that.data.cardList[that.data.index].id;
		let indexData=wx.getStorageSync('indexData');
		let userData=indexData.user;
		let data = {
			'userId': userData.userId,
			'type': userData.type,
			'monthCardId': cardId,
			'deviceCode': userData.deviceCode,
			'deviceId': userData.deviceCode
		};
		let requestUrl = that.data.requestOne ? that.data.monthCardListUrl : that.data.buyMonthCardUrl;
		let _data = {
			url: requestUrl,
			data: data,
			methods: 'post',
			nonelist: true
		};
		utils.request.call(_data, (res) => {
			success(res);
		});
	},
	/*发起微信支付*/
	onBridgeReadyArgments: function(param) {
		let that = this;
		let wxpay=true;
		wx.setStorageSync('wxpay',wxpay);
		wx.requestPayment({
			timeStamp: param.timeStamp,
			nonceStr: param.nonceStr,
			package: param.package,
			signType: param.signType,
			paySign: param.paySign,
			success: function(res) {
				if (res.errMsg == "requestPayment:ok") { // 调用支付成功that.requestOne = true;
					that.data.requestOne=true;
					that.common_request_post(function(res) {
						if (res.data.status == 200) {
							utils.tips('支付成功');
							setTimeout(function() {
								that.getListData();
							}, 800);
						} else {
							utils.tips(res.data.msg);
						};
					});
					wx.removeStorageSync('wxpay');
				} else if (res.errMsg == 'requestPayment:cancel') {
					utils.tips('支付失败！');
					return false;
					wx.removeStorageSync('wxpay');
				};
			},
			fail: function(res) {
				utils.tips('您已取消支付！');
				wx.removeStorageSync('wxpay');
				//fail
			},
			complete: function() {
				//complete
				wx.removeStorageSync('wxpay');
			}
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
